package com.test.alisms;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.test.utils.JsonUtil;

import java.util.HashMap;
import java.util.Map;


public class SmsDemo {

    public static void main(String[] args) throws Exception {
        test2();
    }

    public static void test1() {
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-hangzhou",
                "LTAI4GCHogfrQpdLA58NxbG8",
                "GyAEkG0qTxsU9aZm4P5ojr0imBOzjk");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", "18602463790");
        request.putQueryParameter("SignName", "丹东草莓线上交易大市场");
        request.putQueryParameter("TemplateCode", "SMS_199990232");
        HashMap<String, String> param = new HashMap<>();
        param.put("code", "8866");
        request.putBodyParameter("TemplateParam", JsonUtil.toJson(param));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    public static void test2() throws Exception {
        HashMap<String, String> param = new HashMap<>();
        param.put("code", "8866");
        AliSmsConfig config = AliSmsConfig.builder()
                .regionId("cn-hangzhou")
                .accessKeyId("LTAI4GCHogfrQpdLA58NxbG8")
                .secret("GyAEkG0qTxsU9aZm4P5ojr0imBOzjk")
                .build();
        AliyunSmsHelper smsHelper = new AliyunSmsHelper(config);
        Map<String, Object> result = smsHelper.sendSms("18602463790", "丹东草莓线上交易大市场", "SMS_199990232", param);
        System.out.println(JsonUtil.toJson(result));
    }
}
